home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / aros / exec / insert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-12  |  2.7 KB  |  110 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: insert.c,v 1.5 1996/08/13 13:56:03 digulla Exp $
  4.     $Log: insert.c,v $
  5.     Revision 1.5  1996/08/13 13:56:03  digulla
  6.     Replaced __AROS_LA by __AROS_LHA
  7.     Replaced some __AROS_LH*I by __AROS_LH*
  8.     Sorted and added includes
  9.  
  10.     Revision 1.4  1996/08/01 17:41:13  digulla
  11.     Added standard header for all files
  12.  
  13.     Desc:
  14.     Lang: english
  15. */
  16. #include "exec_intern.h"
  17.  
  18. /*****************************************************************************
  19.  
  20.     NAME */
  21.     #include <exec/lists.h>
  22.     #include <clib/exec_protos.h>
  23.  
  24.     __AROS_LH3I(void, Insert,
  25.  
  26. /*  SYNOPSIS */
  27.     __AROS_LHA(struct List *, list, A0),
  28.     __AROS_LHA(struct Node *, node, A1),
  29.     __AROS_LHA(struct Node *, pred, A2),
  30.  
  31. /*  LOCATION */
  32.     struct SysBase *, SysBase, 39, Exec)
  33.  
  34. /*  FUNCTION
  35.     Insert Node node after pred in list.
  36.  
  37.     INPUTS
  38.     list - The list to insert the node into
  39.     node - This node is to be inserted
  40.     pred - Insert after this node. If this is NULL, node is inserted
  41.         as the first node (same as AddHead()).
  42.  
  43.     RESULT
  44.  
  45.     NOTES
  46.  
  47.     EXAMPLE
  48.     struct List * list;
  49.     struct Node * pred, * node;
  50.  
  51.     // Insert Node node as second node in list
  52.     pred = GetHead (list);
  53.     Insert (list, node, pred);
  54.  
  55.     BUGS
  56.  
  57.     SEE ALSO
  58.     AddHead(), AddTail(), Enqueue(), RemHead(), Remove(), RemTail(),
  59.     "AROS: Exec Lists".
  60.  
  61.     INTERNALS
  62.  
  63.     HISTORY
  64.     26-08-95    digulla created after EXEC-Routine
  65.     26-10-95    digulla adjusted to new calling scheme
  66.  
  67. ******************************************************************************/
  68. {
  69.     __AROS_FUNC_INIT
  70.     assert (node);
  71.     assert (list);
  72.  
  73.     /* If we have a node to insert behind... */
  74.     if (pred)
  75.     {
  76.     /*
  77.         Our successor is the successor of the node we add ourselves
  78.         behind and our predecessor is just the node itself.
  79.     */
  80.     node->ln_Succ = pred->ln_Succ;
  81.     node->ln_Pred = pred;
  82.  
  83.     /*
  84.         We are the predecessor of the successor of our predecessor
  85.         (What ? blblblb... ;) and of out predecessor itself.
  86.         Note that here the sequence is quite important since
  87.         we need ln_Succ in the first expression and change it in
  88.         the second.
  89.     */
  90.     pred->ln_Succ->ln_Pred = node;
  91.     pred->ln_Succ = node;
  92.     }
  93.     else
  94.     {
  95.     /*
  96.         add at the top of the list. I do not use AddHead() here but
  97.         write the code twice for two reasons: 1. The code is small and
  98.         quite prone to errors and 2. If I would call AddHead(), it
  99.         would take almost as long to call the function as the execution
  100.         would take yielding 100% overhead.
  101.     */
  102.     node->ln_Succ           = list->lh_Head;
  103.     node->ln_Pred           = (struct Node *)&list->lh_Head;
  104.     list->lh_Head->ln_Pred = node;
  105.     list->lh_Head           = node;
  106.     }
  107.     __AROS_FUNC_EXIT
  108. } /* Insert */
  109.  
  110.